#include <stdio.h>
#include <stdint.h>
#include "serial_port.h"
#include "serial_manager.h"
#include "serial_save.h"



static const char *TAG = "SerialPort";

static int8_t kSendLen = 40;
static uint8_t kSendData[] =  { 0xAA,0x00 , 0x1f ,0x00, 0x02 , 0xcc, 0xab, 0xcc,  0xab,  0x11,
                                0x11 ,0xcc,0xab, 0xcc, 0xab, 0x00, 0x00, 0x00, 0x00, 0x08, 
                                0x00, 0x10,0x49,0x28,0x61,0x70, 0x5f, 0x62, 0x6c, 0x65, 
                                0x29, 0x3a,0x63, 0x6d, 0x64, 0x3a, 0x32, 0x0a , 0x53,0x55};

int main(int argc, const char* argv[])
{
    int ret;
    int fd;

    int len;
    static FILE *s_fp = NULL;


    RingBuffer recv_buffer;

	printf("Demo [%s]:----->\n", TAG);
    serial_cfg_t conf = {115200, 8,1,'N',0};

    char dev_name[20] = {0};
    serial_process_options(argc, argv, &conf, dev_name);


    printf(" devname %s\n", dev_name);
    serial_conf_print(conf);
    s_fp = fopen(SAVEFILE, "a+");
    if (s_fp == NULL) {
        printf("create file failed.\n");
        return -1;
    }

    ret = serial_port_init(&fd, dev_name, &conf, &recv_buffer);
    if(ret <0)
    {
        perror("serial_setcfg fail.\n");
        return -1;
    }

    serial_write(fd, kSendData, kSendLen);
    while(1)
    {

        len = serial_read(fd, &recv_buffer, 1024);
         
        if(len >0)
        {
            printf("len:%d\n",len);
            ret = serial_manager(&recv_buffer) ;
            if(ret >0)
            {
                serial_write(fd, kSendData, kSendLen);

                if(ret !=1)
                {
                    fprintf( s_fp, "@@@@@@@@@@@ ret:%d\n",ret);

                }
                
            }
        }

    }


    serial_close(fd);
    return 0;
}